package com.xjgc.msibase.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjgc.msibase.entity.MsiExoSystem;
import com.xjgc.msibase.entity.MsiExoSystemServer;
import com.xjgc.msibase.service.IMsiExoSystemServerService;
import com.xjgc.msibase.service.IMsiExoSystemService;
import com.xjgc.msibase.vo.MsiExoSystemPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoDict;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 外围系统注册
 * @Author: 小逗号
 * @Date: 2023-10-30
 * @Version: V1.0
 */
@Api(tags = "外围系统注册")
@RestController
@RequestMapping("/msibase/msiExoSystem")
@Slf4j
public class MsiExoSystemController /*extends JeecgController<MsiRegisterExosystem, IMsiRegisterExosystemService>*/ {
    @Resource
    private IMsiExoSystemService msiExoSystemService;
    @Resource
    private IMsiExoSystemServerService msiExoSystemServerService;

    /**
     * 分页列表查询
     *
     * @param msiExoSystem
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "外围系统注册-分页列表查询")
    @ApiOperation(value = "外围系统注册-分页列表查询", notes = "外围系统注册-分页列表查询")
    @AutoDict
    @GetMapping(value = "/list")
    public Result<IPage<MsiExoSystem>> queryPageList(MsiExoSystem msiExoSystem,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<MsiExoSystem> queryWrapper = QueryGenerator.initQueryWrapper(msiExoSystem, req.getParameterMap());
        Page<MsiExoSystem> page = new Page<MsiExoSystem>(pageNo, pageSize);
        IPage<MsiExoSystem> pageList = msiExoSystemService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping(value = "/queryMsiExoSystemServerByMainId")
    public Result<?> queryMsiExoSystemServerByMainId(String id) {
        Page<MsiExoSystemServer> page = msiExoSystemServerService.page(new Page<MsiExoSystemServer>(0, -1),
                new LambdaQueryWrapper<MsiExoSystemServer>()
                        .eq(MsiExoSystemServer::getExoSystemId, id)
        );

        return Result.OK(page);
    }

    /**
     * 添加
     *
     * @param msiExoSystemPage
     * @return
     */
    @AutoLog(value = "msi_exo_system-添加")
    @ApiOperation(value = "msi_exo_system-添加", notes = "msi_exo_system-添加")
    //@RequiresPermissions("com.xjgc:msi_exo_system:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody MsiExoSystemPage msiExoSystemPage) {
        MsiExoSystem msiExoSystem = new MsiExoSystem();
        BeanUtils.copyProperties(msiExoSystemPage, msiExoSystem);
        msiExoSystemService.saveMain(msiExoSystem, msiExoSystemPage.getMsiExoSystemServerList());
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param msiExoSystemPage
     * @return
     */
    @AutoLog(value = "msi_exo_system-编辑")
    //@RequiresPermissions("com.xjgc:msi_exo_system:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody MsiExoSystemPage msiExoSystemPage) {
        MsiExoSystem msiExoSystem = new MsiExoSystem();
        BeanUtils.copyProperties(msiExoSystemPage, msiExoSystem);
        MsiExoSystem msiExoSystemEntity = msiExoSystemService.getById(msiExoSystem.getId());
        if (msiExoSystemEntity == null) {
            return Result.error("未找到对应数据");
        }
        msiExoSystemService.updateMain(msiExoSystem, msiExoSystemPage.getMsiExoSystemServerList());
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "msi_exo_system-通过id查询")
    @ApiOperation(value = "msi_exo_system-通过id查询", notes = "msi_exo_system-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<MsiExoSystem> queryById(@RequestParam(name = "id", required = true) String id) {
        MsiExoSystem msiExoSystem = msiExoSystemService.getById(id);
        if (msiExoSystem == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(msiExoSystem);

    }
    @AutoDict
    @GetMapping(value = "/exoServerListPop")
    public Result<?> exoServerListPop(String exoSystemCode, String serverType, String exoServerCode,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        LambdaQueryWrapper<MsiExoSystemServer> queryWrapper = new LambdaQueryWrapper<MsiExoSystemServer>().eq(MsiExoSystemServer::getStatus, "1");
        if(StringUtils.isNotBlank(serverType)){
            queryWrapper.eq(MsiExoSystemServer::getExoServerType,serverType);
        }
        if(StringUtils.isNotBlank(exoSystemCode)){
            queryWrapper.like(MsiExoSystemServer::getExoSystemCode,exoSystemCode);
        }
        if(StringUtils.isNotBlank(exoServerCode)){
            queryWrapper.like(MsiExoSystemServer::getExoServerCode,exoServerCode);
        }
        Page<MsiExoSystemServer> page = msiExoSystemServerService.page(new Page<MsiExoSystemServer>(pageNo, pageSize), queryWrapper);
        return Result.OK(page);
    }

}
